//
//  XDPlaceViewController.m
//  Le way walkers
//
//  Created by POP-mac on 15/5/13.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDPlaceViewController.h"
#import "XDPlaceCollectionViewCell.h"
#import "XDCollectionReusableView.h"
#import "XDPlace.h"
#import "XDPlaceDetailsViewController.h"
#import "XDPlaceScenicDetailsViewController.h"
#import "NetworkHandle.h"
#import "UIImageView+WebCache.h"
#import "XDPlaceViewModel.h"
#import "XDPlaceCityViewController.h"
@interface XDPlaceViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *mArr;

@property (nonatomic, retain) NSMutableDictionary *myDIc;

@property (nonatomic, retain) NSMutableArray *arrrrr;

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) UIImageView *gifImageView;

@property (nonatomic, retain) UIView *gifView;

@end

@implementation XDPlaceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self createViews];
    [self yangTuo];
    
}

- (void)getData
{
    
    [NetworkHandle getDataWithURL:@"http://api.breadtrip.com/destination/v3" completion:^(id result) {
        
        NSMutableArray *arr = [result objectForKey:@"elements"];
        
        self.mArr = [NSMutableArray array];
        
        for (NSDictionary * dic in arr) {
            
            XDPlace *xdPlace = [[XDPlace alloc]initWithDic:dic];
            
            [self.mArr addObject:xdPlace];
            
            [xdPlace release];
            xdPlace.dataArr = [NSMutableArray array];
            
            for (NSDictionary *dic1 in xdPlace.data) {
                
                XDPlaceViewModel *xdModel = [[XDPlaceViewModel alloc]initWithDic:dic1];
                
                [xdPlace.dataArr addObject:xdModel];
                
                [xdModel release];
                
            }
            
            NSLog(@"bbbbbb%@", self.mArr);
            
        }
        
        [self.collectionView reloadData];
        
        [self.gifImageView stopAnimating];
        
        self.gifView.hidden = YES;
        
    }];
    
    
}

- (void)yangTuo
{
    [self getData];
    self.gifImageView.hidden = NO;
    self.gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 40, self.view.frame.size.height / 2 - 50, 80, 100)];
    NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"cao1.png"],
                         [UIImage imageNamed:@"cao2.png"],
                         [UIImage imageNamed:@"cao3.png"],
                         [UIImage imageNamed:@"cao4.png"],
                         [UIImage imageNamed:@"cao5.png"],
                         [UIImage imageNamed:@"cao6.png"],
                         [UIImage imageNamed:@"cao7.png"],
                         [UIImage imageNamed:@"cao8.png"],
                         [UIImage imageNamed:@"cao9.png"],
                         [UIImage imageNamed:@"cao10.png"],nil];
    self.gifImageView.animationImages = gifArray; //动画图片数组
    self.gifImageView.animationDuration = 0.8; //执行一次完整动画所需的时长
    self.gifImageView.animationRepeatCount = 10000;  //动画重复次数
    self.gifView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    UIColor *imageB = [UIColor colorWithPatternImage:[UIImage imageNamed:@"123456_123456"]];
    self.gifView.backgroundColor = imageB;
    [self.gifView addSubview:self.gifImageView];
    [self.gifImageView startAnimating];
    [self.view addSubview:self.gifView];
    [_gifImageView release];
    [_gifView release];
}

- (void)createViews
{
    self.title = @"目的地";
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    
    
    self.collectionView =[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowlayout];
    
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 113, 0);
    
    self.collectionView.bounces = NO;
    
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    UIColor *image1 = [UIColor colorWithPatternImage:[UIImage imageNamed:@"123456_123456"]];
    
    self.collectionView.backgroundColor = image1;
    
    flowlayout.itemSize = CGSizeMake((self.view.frame.size.width - 60) / 2, (self.view.frame.size.width - 60) / 2);
    
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    flowlayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 60);
    
    flowlayout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
    
    self.collectionView.delegate = self;
    
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
    
    [_collectionView release];
    
    [flowlayout release];
    
    [self.collectionView registerClass:[XDCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    
    [self.collectionView registerClass:[XDPlaceCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

#pragma mark -- collectionView协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *arr = [[self.mArr objectAtIndex:section] dataArr];
    return arr.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    NSLog(@"1111%ld", self.mArr.count);
    
    return self.mArr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XDPlaceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    XDPlaceViewModel *xdModel = [[self.mArr[indexPath.section] dataArr] objectAtIndex:indexPath.row];
    
    cell.xdModel = xdModel;
    
    
    return cell;
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    XDCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
    NSLog(@"%@", self.mArr[indexPath.row]);
    
    headerView.xdPlace = self.mArr[indexPath.section];
    
    headerView.button.tag = indexPath.section + 10000;
    
    [headerView.button addTarget:self action:@selector(myButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        
//        button.frame = CGRectMake(0, headerView.title.frame.origin.y, headerView.title.frame.size.width, headerView.title.frame.size.height);
//        
//        [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
//        
//        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        
//        [headerView.title addSubview:button];
//        
//        NSLog(@"点击的是第%ld个", indexPath.section);
    
    return headerView;
    
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    XDPlaceScenicDetailsViewController *scenicVC = [[XDPlaceScenicDetailsViewController alloc]init];
    XDPlaceCollectionViewCell *cell = (XDPlaceCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    XDPlaceCityViewController *cityVC = [[XDPlaceCityViewController alloc]init];
    
    scenicVC.typeName = cell.typeName;
    
    scenicVC.ScenicID = cell.ScenicID;
    
    cityVC.ScenicID = cell.ScenicID;
    
    cityVC.typeName = cell.typeName;
    
    if ([scenicVC.typeName integerValue] == 1 || [scenicVC.typeName integerValue] == 2 || [scenicVC.typeName integerValue] == 3) {
        
        [self.navigationController pushViewController:scenicVC animated:YES];
        
    }
    
    if ([scenicVC.typeName integerValue] == 5) {
        
        [self.navigationController pushViewController:cityVC animated:YES];
    }
    
    NSLog(@"wuwuwuwuuwuwuw %@", cityVC.typeName);
    
    NSLog(@"yryryryryyryry %@", cityVC.ScenicID);
    
}

//- (void)buttonAction:(UIButton *)button
//{
//    XDPlaceDetailsViewController *detailsVC = [[[XDPlaceDetailsViewController alloc]init] autorelease];
//    
//    [self.navigationController pushViewController:detailsVC animated:YES];
//}

//- (void)getModelDelegate
//{
//    
//    
//    XDPlaceDetailsViewController *detailsVC = [[[XDPheaderViewlaceDetailsViewController alloc]init] autorelease];
//    
//    [self.navigationController pushViewController:detailsVC animated:YES];
//}

- (void)myButtonAction:(UIButton *)button
{
    if (button.tag - 10000 == 2 || button.tag - 10000 == 5 || button.tag - 10000 == 6) {
        
        XDPlaceDetailsViewController *detailsVC = [[[XDPlaceDetailsViewController alloc]init] autorelease];

       XDPlace *xdPace = [self.mArr objectAtIndex:button.tag - 10000];
        
        detailsVC.num = xdPace.index;
        
        NSLog(@"!!!!!!777777777%@", detailsVC.num);
        
        [self.navigationController pushViewController:detailsVC animated:YES];
        
    }
    else
    {
        NSLog(@"点击没用!");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
